package com.sulent.frame.support.utils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @ClassName UrlResolveUtils
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/1/24 18:23
 * @Version 1.0
 */
public class UrlResolveUtils {

    public static String getHost(String urlStr){
        try {
            URL url = new URL(urlStr);
            return url.getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getPort(String urlStr){
        try {
            URL url = new URL(urlStr);
            int port = url.getPort();
            if (port == -1) {
                port = url.getDefaultPort();
            }
            return port;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getProtocol(String urlStr){
        try {
            URL url = new URL(urlStr);
            return url.getProtocol();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getPath(String urlStr){
        try {
            URL url = new URL(urlStr);
            return url.getPath();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
